package com.gt.xmall.service;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.util.CollectionUtils;
import org.jmesa.facade.TableFacade;
import org.jmesa.limit.Action;
import org.jmesa.limit.Limit;
import org.jmesa.limit.RowSelect;
import org.jmesa.limit.SortSet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;


import com.google.common.collect.Lists;
import com.gt.xmall.service.BaseService;
import com.gt.xmall.util.DynamicSpecifications;
import com.gt.xmall.util.PagerUtil;
import com.gt.xmall.util.SearchBean;

import com.gt.xmall.model.GtdProject;
import com.gt.xmall.repository.GtdProjectDao;
import com.mysql.jdbc.StringUtils;



@Component
@Transactional(readOnly = true)
public class GtdProjectService extends BaseService{
	@Autowired
	private GtdProjectDao gtdProjectDao;
	
	public List<GtdProject> getGtdProjects(){
		return (List<GtdProject>)gtdProjectDao.findAll();
	}
	
	public GtdProject getGtdProject(Long id){
		return gtdProjectDao.findOne(id);
	}
	@Transactional(readOnly = false)
	public GtdProject saveGtdProject(GtdProject gtdProject){
		return gtdProjectDao.save(gtdProject);
	}
	
	@Transactional(readOnly = false)
	public void deleteGtdProject(Long id){
		 gtdProjectDao.delete(id);
	}
	
	public  Long getTotalRow(ServletRequest request){
		Specification<GtdProject> spec = PagerUtil.getSpecification(request, GtdProject.class);
		return gtdProjectDao.count(spec);
	}
	public PageRequest initPageRequest(String tableId,
			HttpServletRequest request, HttpServletResponse response,
			int pageSize ) {
		int initpage=1;
		TableFacade tableFacade = new TableFacade(tableId, request, response);
		;
		Long totalRows = this.getTotalRow(request);
		tableFacade.setTotalRows(totalRows.intValue());
//		log.info("totalRows>>>>>>>>>"+totalRows);
		Limit limit = tableFacade.getLimit();
		SortSet sortSet = limit.getSortSet();
		// Collection<org.jmesa.limit.Sort> sorts=sortSet.getSorts();
		List<org.springframework.data.domain.Sort.Order> _orders = Lists
				.newArrayList();
		
		for (org.jmesa.limit.Sort sort : sortSet.getSorts()) {
			_orders.add(new org.springframework.data.domain.Sort.Order(
					Direction.fromString(sort.getOrder().toParam()), sort
							.getProperty()));
		}
		if(CollectionUtils.isEmpty(_orders)){
			_orders.add(new org.springframework.data.domain.Sort.Order(
					Direction.DESC, "id"));
		}
		// limit.getRowSelect().getPage();
		// limit.getRowSelect().getMaxRows();
		if(!StringUtils.isNullOrEmpty(request.getParameter(tableId+"_"+Action.PAGE.toParam()))){
			initpage = limit.getRowSelect().getPage();
			pageSize = limit.getRowSelect().getMaxRows();
		}
		log.info("initpage========"+initpage);
	
		return new PageRequest(initpage-1,pageSize,
				new org.springframework.data.domain.Sort(_orders));
	}
	public Page<GtdProject> getProjectPage(PageRequest  pageRequest ,Map<String, SearchBean> filters){
		Specification<GtdProject> spec = DynamicSpecifications.bySearchFilter(filters.values(), GtdProject.class);
		
		
		return gtdProjectDao.findAll(spec, pageRequest);
	}
	
}